var watchstart=new Date();
var clocksite=new Array();

var isIE;
var ua = navigator.userAgent.toLowerCase();
isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
var sm;
var isleadopen=false;
function el(object){
	return document.getElementById(object);
}

var sethid;


function createajax(){
 var xmlrequest;
 try
    {
   // Firefox, Opera 8.0+, Safari
    xmlrequest=new XMLHttpRequest();
    }
 catch (e)
    {

  // Internet Explorer
   try
      {
      xmlrequest=new ActiveXObject("Msxml2.XMLHTTP");
      }
   catch (e)
      {

      try
         {
         xmlrequest=new ActiveXObject("Microsoft.XMLHTTP");
         }
      catch (e)
         {
         alert("您的浏览器不支持AJAX！");
         return false;
         }
      }
    }
return xmlrequest;
}

function showhid(obj1,obj2){
	el(obj1).style.display='block';
	el(obj2).style.display='none';
}

function tojson(txt){
	return eval('('+txt+')');
}
function blank(){}
var ajax={
	setmethod:		function(method){
						this.method=method?method:'get';
					},
	setonsuccess:	function(onsuccess){
						this.onsuccess=onsuccess;
					},
	setonloading:	function(onloading){
						this.onloading=onloading?onloading:this.loading;
					},
	send:			function(url,onsuccess,method,params,onloading){
						var xmlhttp=createajax();
						this.setmethod(method);
						this.setonsuccess(onsuccess);
						this.setonloading(onloading);
						var onsuccess=this.onsuccess;
						var ok=this.ok;
						var loading=this.onloading;
						xmlhttp.onreadystatechange=function(){
															if(xmlhttp.readyState == 1) {
																loading('Loading');
																}
															if(xmlhttp.readyState == 4){
																if(xmlhttp.status == 200){
																	var jsonOBJ=tojson(xmlhttp.responseText);
																	ok();
																	xmlhttp=null;
																	onsuccess(jsonOBJ);
																	}
																}
														};
						xmlhttp.open(this.method,url,true);
						if (method=='post')
						{xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
						}
						params=params?params:null;
						xmlhttp.setRequestHeader("If-Modified-Since","0");
						xmlhttp.send(params);
						//ok();
					},
	loading:	function(txt){
					if(!el('ajaxloading')){
						var t=document.createElement('div');
						t.className='loadbg';
						t.innerHTML=txt;
						t.id='ajaxloading';
						document.body.appendChild(t);
						t.style.top=document.documentElement.scrollTop+'px';
						t.style.right=0+'px';
						t.style.display='block';
					}
				},
	ok:			function(){
					if(el('ajaxloading')){document.body.removeChild(el('ajaxloading'));}
				}
};

function creatediv(divid,txt)
{
				var t=document.createElement('div');
				t.className='loadbg';
				t.innerHTML=txt;
				t.id=divid;
				document.body.appendChild(t);
				t.style.top=document.documentElement.scrollTop+'px';
				t.style.right=0+'px';
				t.style.display='block';

}
function deldiv(divobj)
{
document.body.removeChild(el(divobj));
}
function handle_addmail(jsonOBJ){
				var t=document.createElement('div');
				t.className='loadbg';
				t.innerHTML=jsonOBJ['outhtml'];
				t.id='ajaxloading';
				document.body.appendChild(t);
				t.style.top=document.documentElement.scrollTop+'px';
				t.style.right=0+'px';
				t.style.display='block';
				window.setTimeout("deldiv('ajaxloading')",2000);
		}


function ajaxaddmail(cid)
{
	ajax.send('/ajaxaddmail/'+cid+'/',handle_addmail);
}

function handle_issend(jsonOBJ){
	mid=jsonOBJ['mid'];
	issend=jsonOBJ['issend'];
	if(issend)
	el('on'+mid).innerHTML="<img src=\"/static/images/yes.gif\" />";
	else
	el('on'+mid).innerHTML="<img src=\"/static/images/no.gif\" />";
}
function onsending(mid)
{
	ajax.send('/onsending/'+mid+'/',handle_issend);
}

//delete mail from mail list
function handle_delmail(jsonOBJ){
	mid=jsonOBJ['mid'];
	el('onemail'+mid).innerHTML="";
}
function deletemail(mid)
{
	ajax.send('/deletemail/'+mid+'/',handle_delmail);
}

function delmail(mid)
{
	var isgo;
isgo=confirm('Are you sure to delete it?');
if(isgo)
	{
		deletemail(mid);
	}
}


function handle_savetomail(jsonOBJ){
	mid=jsonOBJ['mid'];
	el('cmail'+mid).innerHTML=jsonOBJ['newtomail'];
}
function saveemail()
{
ajax.send('/saveeditmail/',handle_savetomail,'post',"newmail="+el('newmail').value+"&editmid="+el('editmid').value);
}


//send the mail
function handle_gosendthemail(jsonOBJ){
	mid=jsonOBJ['mid'];
	moremail=jsonOBJ['moremail'];
	sendok=jsonOBJ['sendok'];
	if (sendok=='1'||sendok==1)
	{
		//这封邮件已成功发送.在前台列表中隐藏它.
		el('onemail'+mid).innerHTML="";
	}
	else
	{
		//这封邮件发送失败.在前台列表对应提示发送失败
		el('mm'+mid).innerHTML="<img src=\"/static/images/no.gif\" />";
	}
	if (moremail=='1'||moremail==1)
	{
		//可能列表中还有待发送的邮件,几秒后继续发下一封
		creatediv("wait","Waitting 2 seconds to send next mail");
		deldiv("wait");
		window.setTimeout("goemail(1)",2000);
	}
	else
	{
		//列表中已无待发邮件.提示发送完成
		creatediv("finish","All the mails have been sent");
		window.setTimeout("deldiv('finish')",2000);
	}
}

function goemail(id)
{
	if(el('subject').value=='')
	{
		el('somewrong').innerHTML="<font color='red'><img src='/static/images/warning_small.gif' />Enter mail subject please</font>";
	}
	else if(el('content').value=='')
	{
		el('somewrong').innerHTML="<font color='red'><img src='/static/images/warning_small.gif' />Enter mail content please</font>";
	}
	else
	{
	ajax.send('/sendthemail/',handle_gosendthemail,'post',"frommail="+el('frommail').value+"&subject="+el('subject').value+"&content="+el('content').value);
	}
}


//show and hidden box
function showbox(boxobj)
{
el(boxobj).style.display="block";
}
function hibox(obxobj)
{
el(obxobj).style.display="none";
}

function notice()
{
	if(el("helpbox").style.display=="block")
	{
		el("helpbox").style.display="none";
	}
	else
	{
		el("helpbox").style.display="block";
	}
}
// edit mail
function editmail(mid,mailaddress)
{
showbox('editbox');
el("newmail").value=mailaddress;
el("editmid").value=mid;
}


// save up load file
function handle_delmailfile(jsonOBJ)
{
delok=jsonOBJ['delok'];
	if(delok==1||delok=='1')
	{
		el("fileurl").value="";
		//alert("success to delete");
	}
	else if(delok==0||delok=='0')
	{
		alert("Fail to delete");
	}
}

function delfile(ma)
{
ajax.send('/delfile/',handle_delmailfile,'post',"ma="+ma);
}




function handle_delatmail(jsonOBJ){
	mid=jsonOBJ['mid'];
	el('atmailfile').innerHTML="";
}
function deletethefile(mid)
{
	ajax.send('/delatmailfile/'+mid+'/',handle_delatmail);
}

function delatmailfile(mid)
{

	var isgo;
	isgo=confirm('Are you sure to delete the file?');
	if(isgo)
	{
		deletethefile(mid);
	}
}

//add new mail address
function checkemail(mail)
{
	var myreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
	if(!myreg.test(mail))
	{
	alert('warning\n\nThe email address is invalid！');
	return false;
	}
	else
	{
		return true;
	}
}


function handle_newmail(jsonOBJ){
	var havein;
	havein=jsonOBJ['havein'];
	if (havein==1||havein=='1')
	{
				var t=document.createElement('div');
				t.className='loadbg';
				t.innerHTML=jsonOBJ['errinfo'];
				t.id='ajaxloading';
				document.body.appendChild(t);
				t.style.top=document.documentElement.scrollTop+'px';
				t.style.right=0+'px';
				t.style.display='block';
				window.setTimeout("deldiv('ajaxloading')",3000);
	}
	else
	{
	el('allmaillist').innerHTML=jsonOBJ['outhtml'];
	}
}
function addmail()
{
	var nd;
	nd=el('mailaddr').value;
	if(nd=='')
	{
		alert("Enter a email address please");
	}
	else
	{
		if(checkemail(nd))
		{
			ajax.send('/saveaddmail/',handle_newmail,'post',"mailaddress="+nd);
		}
	}
}